package cn.cena.user.service.impl;

import cn.cena.basic.Exception.MyException;
import cn.cena.basic.query.BaseQuery;
import cn.cena.basic.service.impl.BaseServiceImpl;
import cn.cena.basic.util.MD5Utils;
import cn.cena.basic.util.PageList;
import cn.cena.basic.util.StrUtils;
import cn.cena.user.Constant.UserConstant;
import cn.cena.user.domain.Logininfo;
import cn.cena.user.domain.User;
import cn.cena.user.dto.Userdto;
import cn.cena.user.mapper.LogininfoMapper;
import cn.cena.user.mapper.UserMapper;
import cn.cena.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(Userdto userdto) {
        // 判断是否为空
        if(StringUtils.isEmpty(userdto.getPhone())
                || StringUtils.isEmpty(userdto.getVerifyCode())
                || StringUtils.isEmpty(userdto.getPassword())
                || StringUtils.isEmpty(userdto.getPasswordRepeat())){
                throw new MyException("请完善注册信息");
        }
        // 手机号是否被注册
        User tmpuser = userMapper.findByphone(userdto.getPhone());
        if (tmpuser != null){
            throw new MyException("手机号已注册，请直接登录……");
        }
        // 判断验证码是否过期
        Object tmpCode = redisTemplate.opsForValue().get(UserConstant.REGISTER_PREFIX + userdto.getPhone());

        if(tmpCode == null){
            throw new MyException("验证码已过期，请重新发送");
        }
        // 若未过期，判断验证码是否正确
        String code = tmpCode.toString().split("-")[1];
        if(!userdto.getVerifyCode().equalsIgnoreCase(code)){ // 不区分大小写equalsIgnoreCase
            throw new MyException("验证码错误，请重新输入");
        }

        // 判断两次密码是否一致
        if(!userdto.getPassword().equals(userdto.getPasswordRepeat())){
            throw new MyException("两次密码不一致");
        }

        // 校验完成，开始注册
        User user = userdto2user(userdto);

        Logininfo logininfo = user2logininfo(user);

        // 将信息存入logininfo表
        logininfoMapper.save(logininfo);

        // 存进user表
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);

    }

    private Logininfo user2logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        // 按照同名属性进行拷贝
        BeanUtils.copyProperties(user,logininfo );
        return logininfo;
    }

    private User userdto2user(Userdto userdto) {
        User user = new User();
        user.setUsername(userdto.getPhone());
        user.setPhone(userdto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        // 设置盐值
        user.setSalt(salt);
        // 加盐加密
        String psw = MD5Utils.encrypByMd5(salt + userdto.getPassword());
        user.setPassword(psw);
        return user;
    }
}
